<?php

/**
 *
 * 打印接口日志
 *
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/8
 * Time: 10:27
 */
class XhprofMiddleware
{
	public $xhprof_enable = false;
	public function __construct() {
		ini_set("display_errors", true);
		$this->xhprof_enable = @$_GET['xhprof_enable'] == '1' && function_exists('xhprof_enable');
	}
    public function init()
    {
		if (!$this->xhprof_enable) {
			return;
		}
        if ($this->xhprof_enable) {
            xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);
        }
    }
    public function __destruct() {
    	if ($this->xhprof_enable) {
    		$xhprof_data = xhprof_disable();
    		$xhprof_dir = dirname(__FILE__)."/output/";
    		include_once $xhprof_dir."xhprof_lib/utils/xhprof_lib.php";
    	
    		include_once $xhprof_dir."xhprof_lib/utils/xhprof_runs.php";
    		$xhprof_runs = new \XHProfRuns_Default();
    	
    		$xhprof_runs->save_run($xhprof_data, "" . mt_rand() . "");
    	}
    }
}
$XhprofMiddleware = new XhprofMiddleware();
$XhprofMiddleware->init();